<?php

namespace Admin\Controller;

class BatchNoController extends AdminbaseController {

    function _initialize() {
        parent::_initialize();
        $this->currentModel = D("BatchNo");
    }

    //首页
    public function index() {
        $requestMap = $this->convertRequestToMap();

        //分页
        $count = $this->currentModel->getListCount($requestMap);
        $page = $this->page($count, C("DEFAULT_PAGE_SIZE"));

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $requestMap["bn.enterprise_code"] = $user["enterprise_code"];
        //获取列表
        $list = $this->currentModel->queryForList($page, $requestMap);

//        $this->assign("statusEnum", C("DEFAULT_YN"));
        $this->assign('list', $list);

        $this->assign("page", $page->show('Admin', $this->parameter));
        $this->display();

    }
    public function remove() {

        $id = $_REQUEST['id'];
        $this->currentModel->deleteById($id);

        $this->success('删除成功!', U("BatchNo/index"));

    }

    public function add() {
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $number = getBatchNo($user["enterprise_code"]);
        $batchNo = "00000000".substr(date('Ymd'),2,6).substr(strval($number+1000000),1,6);
        $info["batch_no"] = $batchNo;
        if($number < 10){
            $auditNo = "CP001-".date('Y')."-".substr(strval($number+100),1,2);
        }else{
            $auditNo = "CP001-".date('Y')."-".$number;
        }
        $info["audit_no"] = $auditNo;
        $info["date"] = date('Y-m-d');

        $this->assign('info', $info);
        $this->display("add");
    }

    /**
     * 添加数据
     */
    public function addPost() {
        $data["patient"] = $_REQUEST["patient"];
        $data["type"] = $_REQUEST["type"];
        $data["date"] = date('Y-m-d');
        $data["create_time"] = getCurrentDate();
        $data["update_time"] = getCurrentDate();
        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["enterprise_code"] = $user["enterprise_code"];
        $data["update_id"] = $user["employee_id"];
        $number = getBatchNo($user["enterprise_code"]);
        $data["number"] = $number;
        $batchNo = "00000000".substr(date('Ymd'),2,6).substr(strval($number+1000000),1,6);
        $data["batch_no"] = $batchNo;
        if($number < 10){
            $auditNo = "CP001-".date('Y')."-".substr(strval($number+100),1,2);
        }else{
            $auditNo = "CP001-".date('Y')."-".$number;
        }
        $data["audit_no"] = $auditNo;

        $this->currentModel->add($data);

        $this->success("添加成功", U("BatchNo/index"));

    }

    public function edit() {
        $id = $_REQUEST["id"];
        $info = D("BatchNo")->queryById($id);
        $this->assign("info", $info);
        $this->display("edit");
    }
    /**
     * 更新数据
     */
    public function editPost() {

        $id = $_REQUEST["id"];
        $data = $this->currentModel->queryById($id);
        $data["patient"] = $_REQUEST["patient"];
        $data["type"] = $_REQUEST["type"];
        $data["update_time"] = getCurrentDate();

        $users_obj= M("admin");
        $userId=$_SESSION['ADMIN_ID'];
        $user=$users_obj->where("user_id=$userId")->find();
        $data["update_id"] = $user["employee_id"];

        $this->currentModel->updateInfo($data);

        $this->success("更新成功", U("BatchNo/index"));

    }
}